package com.ytc.media.controller;

import com.aliyuncs.exceptions.ClientException;
import com.ytc.commons.model.dto.*;
import com.ytc.commons.model.vo.MediaVo;
import com.ytc.commons.model.vo.ResourceVo;
import com.ytc.commons.model.vo.ResponseResult;
import com.ytc.commons.model.vo.VodUploadVo;
import com.ytc.media.domain.Media;
import org.springframework.web.bind.annotation.*;
import com.ytc.media.service.MediaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * <p>
 * 媒资信息前端控制器
 * </p>
 *
 * @author jiuye145
 */
@Slf4j
@RestController
@RequestMapping("/media")
public class MediaController {

    @Autowired
    private MediaService  mediaService;


    /*
    * //获取上传凭证
    * POST /vod-token
    * 参数VodUploadDto
    * 响应ResponseResult<VodUploadVo>
    * */
    @PostMapping("/vod-token")
    public ResponseResult<VodUploadVo> creatToken(@RequestBody VodUploadDto vodUploadDto)  throws Exception {

        return mediaService.creatToken(vodUploadDto);
    }
    /*
     * //刷新上传凭证
     *GET /refresh-vod-token/{videoId}
     * 参数：videoId
     * 响应ResponseResult<VodUploadVo>
     * */
     @GetMapping("/refresh-vod-token/{videoId}")
    public ResponseResult<VodUploadVo> refreshToken(@PathVariable("videoId") String videoId)throws Exception{
         return mediaService.refreshToken(videoId);

     }

     //保存视频
        @PostMapping
        public ResponseResult saveMedia(@RequestBody MediaDto dto)throws Exception{
            return mediaService.saveMedia(dto);
    }


    /*
    * //分页查询视频列表
    * Post /list
    * 请求参数：
    * PageRequest
    * MediaQueryDto
    * 响应
    * PageResponseResult<MediaVo>
    * */
    @PostMapping("/list")
    public ResponseResult<MediaVo> list(PageRequest request, @RequestBody MediaQueryDto dto){
        return mediaService.findAll(request,dto);

    }
   /*
   * //视频预览操作
   *  请求 GET       /media/preview/{mediaId}
   * 返回参数ResponseResult<String url>
   * */
   @GetMapping("/preview/{mediaId}")
    public ResponseResult<String> previewMedia(@PathVariable("mediaId") Long mediaId)throws Exception{
       return mediaService.previewMedia(mediaId);
   }


    /*
     * //视频移除操作 (逻辑删除) 也需要删除阿里云的视频（没写）
     *  请求 DELETE    /media/{mediaId}
     * 返回参数ResponseResult
     * */
    @DeleteMapping("/{mediaId}")
    public ResponseResult<Boolean> deleteMedia(@PathVariable("mediaId") Long mediaId){
        return mediaService.deleteMedia(mediaId);
    }


    /*
    * //根据id查询视频
    * */
    @GetMapping("/{mediaId}")
    public ResponseResult<MediaVo> findById(@PathVariable("mediaId")Long mediaId){

        return mediaService.findById(mediaId);
    }

    /*
    * //管理员审核视频
    * PUT media/media/audit、
    * 请求参数：MediaAuditDto
    * 返回值：null
    * */
    @PutMapping("/audit")
    public ResponseResult auditMedia(@RequestBody MediaAuditDto dto){
        return mediaService.auditMedia(dto);
    }

    //根据媒资id查询播放地址
    @GetMapping("/media-url/{mediaId}")
    public ResponseResult<ResourceVo> findMediaUrl(@PathVariable("mediaId") Long mediaId) throws Exception {

        return mediaService.findMediaUrl(mediaId);
    }
}
